{% extends "layout.html" %}

{% macro DisplayGraph(name, layout, width, height, color, data_variable, options) %}
  <div><canvas id="canvas{{ name }}" height="{{ height }}" width="{{ width }}"></canvas></div>
  <script>
    var options{{ name }} = {
      "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[{{ color }}]),
      {% for k, v in options.iteritems() %}"{{ k }}": {{ v }},
{% endfor %}
    };

    function draw{{ name }}() {
      var layout = new PlotKit.Layout("{{ layout }}", options{{ name }});
      layout.addDataset("data", {{ data_variable }});
      layout.evaluate();
      var canvas = MochiKit.DOM.getElement("canvas{{ name }}");
      var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options{{ name }});
      plotter.render();
    }

    MochiKit.DOM.addLoadEvent(draw{{ name }});
  </script>
{% endmacro %}

{% block header -%}
{{ super() }}
<script type="text/javascript" src="../mochikit/MochiKit.js"></script>
<script type="text/javascript" src="../plotkit/Base.js"></script>
<script type="text/javascript" src="../plotkit/Layout.js"></script>
<script type="text/javascript" src="../plotkit/Canvas.js"></script>
<script type="text/javascript" src="../plotkit/SweetCanvas.js"></script>
{% endblock header %}

{% block content %}
<script type='text/javascript'>
  var buildTimes = [
    {% for t in buildTimes %}[{{ loop.index0 if data_in_forward_order is defined else loop.revindex0 }}, {{ t[1] }}]{% if not loop.last %},{% endif %}        // build {{ t[0] }}
{% endfor %}  ];

  var failingSteps = [
    {% for count in failingSteps.values() %}[{{ loop.index0 }}, {{ count }}]{% if not loop.last %},
    {% endif %}{% endfor %}];

  var failingStepsLabel = [
    {% for step in failingSteps %}{v:{{ loop.index0 }}, label:"{{ step }}"}{% if not loop.last %},
    {% endif %}{% endfor %}];

  var ratioSuccessFailures = [[0, {{ numberOfSuccess }}], [1, {{ numberOfFailures }}]];

  var stepTimeLabel = [
    {% for step in stepTimes %}{v:{{ loop.index0 }}, label:"{{ step }}"}{% if not loop.last %},
    {% endif %}{% endfor %}];

  var stepTimeAvg = [
    {% for v in stepTimes.itervalues() %}[{{ loop.index0 }}, {{ v|extract_index(1)|average }}]{% if not loop.last %},
    {% endif %}{% endfor %}];

{% for k, v in stepTimes.iteritems() %}
  var stepTimes{{ k }} = [
    {% for steptime in v %}[{{ loop.index0 if data_in_forward_order is defined else loop.revindex0 }}, {{ steptime[1] }}]{% if not loop.last %},{% endif %}        // build {{ steptime[0] }}
{% endfor %}  ];

{% endfor %}
</script>
<center>
<h1> {{ builder_status.getName() }} </h1><br>
<h2> Cycle time (minutes)</h2>
  {{ DisplayGraph("cycleTimeBar", "line", 1200, 300, 0, "buildTimes", {"drawXAxis": "false"}) }}

<table><tr>
  <td>
    <h2>Ratio Success/Failures</h2><br>
    {{ DisplayGraph("ratioSuccessFailurePie", "pie", 380, 300, 4, "ratioSuccessFailures", {"xTicks": "[{v:0, label:'Success'}, {v:1, label:'Failures'}]"}) }}
  </td>
  <td>
    <h2>Failures by steps</h2><br>
    {{ DisplayGraph("stepFailuresPie", "pie", 380, 300, 1, "failingSteps", {"xTicks": "failingStepsLabel"}) }}
  </td>
  <td>
    <h2>Average step time distribution</h2><br>
    {{ DisplayGraph("stepAvgTime", "pie", 380, 300, 2, "stepTimeAvg", {"xTicks": "stepTimeLabel"}) }}
  </td>
</tr></table>

<table><tr>
{%- for k, v in stepTimes.iteritems() %}
  <td>
    <h3>Cycle time (seconds): {{ k }}</h3><br>
    {{ DisplayGraph("stepsTime%sLine" % k|fixname, "line", 400, 300, colorMap.get(k|fixname, 0), "stepTimes%s" % k, {"drawXAxis": "false", "yAxis": "[0.00, %d.00]" % (v|extract_index(1)|max|int+1), "yTicks": "[%s]" % ", ".join((v|extract_index(1)|max|int + 1)|ticks)}) }}
  </td>
{%- if not loop.index % 3 %}
</tr><tr>
{% endif -%}
{% endfor %}
</tr></table></center>
{% endblock content %}
